const reqlib = require('app-root-path').require
const Flag = reqlib('/app/common/btb/flag')

function buySellFlag (btbItem, ifCheckDay) {
  const dayFlagList = []
  for (let i = btbItem.dayInfo.length - 10; i < btbItem.dayInfo.length; i++) {
    const todayInfo = btbItem.dayInfo[i]
    const netChangeRatioListDayAll = []
    btbItem.dayInfo.forEach((v, index) => {
      if (index <= i) {
        netChangeRatioListDayAll.push(v.netChangeRatio)
      }
    })
    const dayFlag = new Flag({
      key: btbItem.code,
      ema5: todayInfo.ema5,
      ema10: todayInfo.ema10,
      ema20: todayInfo.ema20,
      kdj: todayInfo.kdj,
      kline: {
        ...todayInfo
      },
      netChangeRatioList: netChangeRatioListDayAll.slice(netChangeRatioListDayAll.length - 40)
    })
    dayFlagList.push(dayFlag)
  }
  const todayFlag = dayFlagList[dayFlagList.length - 1]
  // 0:买入，1:卖出
  /**
   * ------------------- 买入
   */
  const flagList = []
  if (todayFlag.buyManyDownBig()) {
    let rate = 1 / 3
    if (btbItem.code === 'BTC') {
      rate = 1 / 5
    }
    flagList.push({
      type: 0,
      interval: 'd',
      rate
    })
  }
  if (!todayFlag.buyManyDownBig() && todayFlag.buyManyDown()) {
    let rate = 1 / 6
    if (btbItem.code === 'BTC') {
      rate = 1 / 10
    }
    flagList.push({
      type: 0,
      interval: 'd',
      rate
    })
  }
  if (todayFlag.buyOneDeepDown()) {
    let rate = 1 / 3
    if (btbItem.code === 'BTC') {
      rate = 1 / 5
    }
    flagList.push({
      type: 0,
      interval: 'd',
      rate
    })
  }
  if (todayFlag.buyOpenIsHigh1()) {
    flagList.push({
      type: 0,
      interval: 'd',
      rate: 1 / 3
    })
  }
  /**
   * -------------------卖出
   */
  if (todayFlag.sellLowUp(-6)) {
    flagList.push({
      type: 1,
      interval: 'd',
      rate: 1 / 2
    })
  }
  if (todayFlag.sellLowUp(-4)) {
    flagList.push({
      type: 1,
      interval: 'd',
      rate: 1 / 6
    })
  }
  if (todayFlag.sellTest2() || todayFlag.sellTest3() || todayFlag.sellTest4() || todayFlag.sellTest5() || todayFlag.sellTest6()) {
    flagList.push({
      type: 1,
      interval: 'd',
      rate: 1 / 6
    })
  }
  if (todayFlag.sellLowWave()) {
    if (todayFlag.ema5 < todayFlag.ema10) {
      flagList.push({
        type: 1,
        interval: 'd',
        rate: 1 / 2
      })
    } else {
      flagList.push({
        type: 1,
        interval: 'd',
        rate: 1 / 6
      })
    }
  }
  if (todayFlag.sellOpenIsLow(0.1)) {
    flagList.push({
      type: 1,
      interval: 'd',
      rate: 1 / 2
    })
  }
  if (todayFlag.sellOpenIsLow(0.3)) {
    flagList.push({
      type: 1,
      interval: 'd',
      rate: 1 / 4
    })
  }
  /**
   * ------------------过滤
   */
  const fList = []
  const buy = {
    type: 0,
    rate: 0
  }
  const sell = {
    type: 1,
    rate: 0
  }
  flagList.forEach((v) => {
    if (v.type === 0) {
      if (
        (v.interval === 'd' && ifCheckDay) ||
        v.interval === 'h'
      ) {
        if (v.rate > buy.rate) {
          buy.rate = v.rate
        }
      }
    } else if (v.type === 1) {
      if (
        (v.interval === 'd' && ifCheckDay) ||
        v.interval === 'h'
      ) {
        if (v.rate > sell.rate) {
          sell.rate = v.rate
        }
      }
    }
  })
  if (sell.rate) {
    fList.push(sell)
  }
  if (!sell.rate && buy.rate) {
    fList.push(buy)
  }
  return fList
}

exports.buySellFlag = buySellFlag
